package io.github.center.config;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.github.center.config.typehandler.CommonEnumTypeHandler;
import io.github.linpeilie.Converter;
import io.github.linpeilie.annotations.ComponentModelConfig;
import lombok.RequiredArgsConstructor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.text.SimpleDateFormat;

/**
 * 应用自动配置接口
 *
 * @author zhonghaijun
 * @version 1.0.0
 * @email "mailto:zhonghaijun@fkhwl.com"
 * @date 2022.11.30 15:15
 * @since 1.0.0
 */
@Configuration
@EnableConfigurationProperties(ApplicationProperties.class)
@MapperScan("io.github.center.mapper")
@ComponentModelConfig(componentModel = "default")
@RequiredArgsConstructor
public class ApplicationAutoConfiguration {

    /**
     * 应用程序属性
     */
    private final ApplicationProperties applicationProperties;

    /**
     * 添加跨域
     *
     * @return the filter registration bean
     * @since 1.0.0
     */
    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilter() {
        //1. 添加 CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        //放行哪些原始域
        config.addAllowedOrigin(CorsConfiguration.ALL);
        //是否发送 Cookie
        config.setAllowCredentials(true);
        //放行哪些请求方式
        config.addAllowedMethod(CorsConfiguration.ALL);
        //放行哪些原始请求头部信息
        config.addAllowedHeader(CorsConfiguration.ALL);
        //暴露哪些头部信息
        // config.addExposedHeader(CorsConfiguration.ALL);
        //2. 添加映射路径
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**", config);
        //3. 返回新的CorsFilter
        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(corsConfigurationSource));
        bean.setOrder(0);
        bean.addUrlPatterns("/*");
        return bean;
    }

    /**
     * 配置mybatis plus自定义的枚举类型转换器
     *
     * @return configuration customizer
     * @since 1.0.0
     */
    @Bean
    public ConfigurationCustomizer enumConfigurationCustomizer() {
        return configuration -> configuration.setDefaultEnumTypeHandler(CommonEnumTypeHandler.class);
    }

    /**
     * 自定义序列化器
     *
     * @return the object mapper
     * @since 1.0.0
     */
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        SimpleDateFormat format = new SimpleDateFormat();
        mapper.setDateFormat(format);
        mapper.registerModule(new JavaTimeModule());
        mapper.registerModule(new Jdk8Module());
        return mapper;
    }

    /**
     * Converter
     *
     * @return the converter
     * @since 1.0.0
     */
    @Bean
    public Converter converter() {
        return new Converter();
    }

}
